// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "appname": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "软件名称"
  },
  "WebUrl": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "H5手机网站地址"
  },
  "APIjiekoUrl": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "API接口地址"
  },
  "About": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "关于我们"
  },
  "ICPbeanhao": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "ICP备案号"
  },
  "adminQQ": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "QQ号"
  },
  "adminWX": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "微信号"
  },
  "adminAlipay": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "支付宝"
  },
  "adminEmail": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "Email邮箱"
  },
  "appUser_jieshao": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "App介绍"
  },
  "app_iconImg_file": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "file"
      }
    ],
    "label": "app图标"
  },
  "app_Web_appdownload": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "app下载地址"
  },
  "share_appSDK_type": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "调用社交sdk分享,需在manifest.json的sdk配置中配的分享sdk厂商，与本机安装了什么社交App无关",
            "value": 1
          },
          {
            "text": "调用系统分享组件发送分享消息，不需要配置分享SDK",
            "value": 2
          }
        ]
      }
    ],
    "label": "分享调用SDK类型",
    "defaultValue": 2
  },
  "share_title": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "分享内容的标题"
  },
  "share_imageUrl_file": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "file"
      }
    ],
    "label": "分享内容的图片"
  },
  "share_summary": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "分享内容的摘要"
  },
  "share_type": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "图文",
            "value": 1
          },
          {
            "text": "纯文字",
            "value": 2
          },
          {
            "text": "纯图片",
            "value": 3
          }
        ]
      }
    ],
    "label": "分享类型",
    "defaultValue": 1
  },
  "share_href": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "分享跳转链接"
  },
  "share_erweimUrl_file": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "file"
      }
    ],
    "label": "分享二维码"
  },
  "App_Player_zyjx_jieko": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "解析接口"
  }
}

const enumConverter = {
  "share_appSDK_type_valuetotext": {
    "1": "调用社交sdk分享,需在manifest.json的sdk配置中配的分享sdk厂商，与本机安装了什么社交App无关",
    "2": "调用系统分享组件发送分享消息，不需要配置分享SDK"
  },
  "share_type_valuetotext": {
    "1": "图文",
    "2": "纯文字",
    "3": "纯图片"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
